package client;

import constants.GameConstants;
import server.life.MapleLifeFactory;
import server.quest.MapleQuest;
import tools.FileoutputUtil;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MapleQuestStatus implements Serializable {
    private static long serialVersionUID = 91795419934134L;
    private transient MapleQuest quest;
    private byte status;
    private Map<Integer, Integer> killedMobs;
    private int npc;
    private long completionTime;
    private int forfeited;
    private String customData;

    public MapleQuestStatus(MapleQuest quest, int status) {
        this.killedMobs = null;
        this.forfeited = 0;
        this.quest = quest;
        this.setStatus((byte) status);
        this.completionTime = System.currentTimeMillis();
        if (status == 1 && !quest.getRelevantMobs().isEmpty()) {
            this.registerMobs();
        }
    }

    public MapleQuestStatus(MapleQuest quest, byte status, int npc) {
        this.killedMobs = null;
        this.forfeited = 0;
        this.quest = quest;
        this.setStatus(status);
        this.setNpc(npc);
        this.completionTime = System.currentTimeMillis();
        if (status == 1 && !quest.getRelevantMobs().isEmpty()) {
            this.registerMobs();
        }
    }

    public MapleQuest getQuest() {
        return this.quest;
    }

    public byte getStatus() {
        return this.status;
    }

    public void setStatus(byte status) {
        this.status = status;
    }

    public int getNpc() {
        return this.npc;
    }

    public void setNpc(int npc) {
        this.npc = npc;
    }

    public boolean isCustom() {
        return GameConstants.isCustomQuest(this.quest.getId());
    }

    private void registerMobs() {
        this.killedMobs = (Map<Integer, Integer>) new LinkedHashMap();
        Iterator<Integer> iterator = this.quest.getRelevantMobs().keySet().iterator();
        while (iterator.hasNext()) {
            int i = ((Integer) iterator.next()).intValue();
            this.killedMobs.put(Integer.valueOf(i), Integer.valueOf(0));
        }
    }

    private int maxMob(int mobid) {
        for (Entry<Integer, Integer> qs : this.quest.getRelevantMobs().entrySet()) {
            if (((Integer) qs.getKey()).intValue() == mobid) {
                return ((Integer) qs.getValue()).intValue();
            }
        }
        return 0;
    }

    public boolean mobKilled(int id, int skillID) {
        try {
            if (this.quest != null && this.quest.getSkillID() > 0 && this.quest.getSkillID() != skillID) {
                return false;
            }
            Integer mob = this.killedMobs.get(id);
            if (mob != null) {
                int mo = this.maxMob(id);
                if (mob >= mo) {
                    return false;
                }
                this.killedMobs.put(id, Math.min(mob + 1, mo));
                return true;
            } else {
                for (Entry<Integer, Integer> mo2 : this.killedMobs.entrySet()) {
                    if (this.questCount(mo2.getKey(), id)) {
                        int mobb = this.maxMob(mo2.getKey());
                        if (mo2.getValue() >= mobb) {
                            return false;
                        }
                        this.killedMobs.put(mo2.getKey(), Math.min(mo2.getValue() + 1, mobb));
                        return true;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            FileoutputUtil.outError("logs/杀死怪物计次异常.txt", (Throwable) ex);
        }
        return false;
    }

    private boolean questCount(int mo, int id) {
        if (MapleLifeFactory.getQuestCount(mo) != null) {
            Iterator<Integer> iterator = MapleLifeFactory.getQuestCount(mo).iterator();
            while (iterator.hasNext()) {
                int i = ((Integer) iterator.next()).intValue();
                if (i == id) {
                    return true;
                }
            }
        }
        return false;
    }

    public void setMobKills(int id, int count) {
        if (this.killedMobs == null) {
            this.registerMobs();
        }
        this.killedMobs.put(Integer.valueOf(id), Integer.valueOf(count));
    }

    public boolean hasMobKills() {
        return this.killedMobs != null && this.killedMobs.size() > 0;
    }

    public int getMobKills(int id) {
        Integer mob = (Integer) this.killedMobs.get(Integer.valueOf(id));
        if (mob == null) {
            return 0;
        }
        return mob.intValue();
    }

    public Map<Integer, Integer> getMobKills() {
        return this.killedMobs;
    }

    public long getCompletionTime() {
        return this.completionTime;
    }

    public void setCompletionTime(long completionTime) {
        this.completionTime = completionTime;
    }

    public int getForfeited() {
        return this.forfeited;
    }

    public void setForfeited(int forfeited) {
        if (forfeited >= this.forfeited) {
            this.forfeited = forfeited;
            return;
        }
        throw new IllegalArgumentException("Can't set forfeits to something lower than before.");
    }

    public void setCustomData(String customData) {
        this.customData = customData;
    }

    public String getCustomData() {
        return this.customData;
    }
}
